<?php


namespace app\cms\home\api;

use app\cms\community\thirdPartyApi\abstracts\Config;
use app\cms\community\thirdPartyApi\payPal\lib\publicParams;
use app\cms\community\thirdPartyApi\weChat\lib\payParams;
use iflow\auth\lib\authAnnotation;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\PostMapping;

#[Controller('cms/api/home/pay')]
class pay {

    /**
     * 微信支付
     * @param string $type
     * @param int $total_fee
     * @return Response
     */
    #[PostMapping]
    #[authAnnotation('pay', '*')]
    public function wechatPay(string $type, int $total_fee): Response
    {
        try {
            $params = new payParams(Config::WECHAT_TYPE);
            $params -> body = "订购商品 - 在线支付";
            $params -> total_fee = $total_fee;
            $params -> open_id = "";
            return (new \app\cms\community\thirdPartyApi\weChat\Pay($params)) -> createOrder($type);
        } catch (\Exception $exception) {
            return message() -> nodata('创建支付订单失败'. $exception -> getMessage());
        }
    }

    /**
     * 微信支付通知回调地址
     * @return Response
     * @throws \Exception
     */
    #[PostMapping]
    public function notify(): Response {
        $params = new payParams(Config::WECHAT_TYPE);
        return (new \app\cms\community\thirdPartyApi\weChat\Pay($params)) -> notify(function ($result): Response {
            // 回调处理方法
            return message() -> success('支付成功');
        });
    }

    /**
     * pal pay 支付 （含 回调）
     * @param string $type 支付类型 JsApiPay 为 回调 、 H5 为创建支付
     * @return Response
     * @throws \Exception
     */
    #[PostMapping]
    #[authAnnotation('pay', '*')]
    public function payPalPay(string $type): Response
    {
        $params = new publicParams(Config::PAYPAL);
        $params -> setOrderID(request() -> postParams('orderID') ?: '');
        return (new \app\cms\community\thirdPartyApi\payPal\pay($params)) -> createOrder($type);
    }
}